package com.fox.imagedb.gui;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.StringTokenizer;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import net.miginfocom.swing.MigLayout;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Metadata;
import com.drew.metadata.jpeg.JpegCommentDescriptor;
import com.drew.metadata.jpeg.JpegCommentDirectory;
import com.fox.imagedb.gui.components.TextField;

public class DataPanel extends JScrollPane {

	private static final long serialVersionUID = 1L;

	private TextField den;

	private TextField date;

	public DataPanel() {
		super();
		getVerticalScrollBar().setUnitIncrement(20);
		den = new TextField("Taniere");
		date = new TextField("Date");

		JPanel panel = new JPanel(new MigLayout());

		den.addToPanel(panel);
		date.addToPanel(panel);
		panel.add(new JLabel("test1"));
		panel.add(new JLabel("test2"));
		panel.add(new JLabel("test3"), "wrap");
		

		setViewportView(panel);
	}

	// FIXME : deplacer dans une classe au niveau hierarchique superieur.
	public void loadData(File image) {
		try {
			Metadata metadata = ImageMetadataReader.readMetadata(image);
			JpegCommentDirectory directory = metadata
					.getDirectory(JpegCommentDirectory.class);
			JpegCommentDescriptor descriptor = new JpegCommentDescriptor(
					directory);
			String coms = descriptor.getJpegCommentDescription();
			HashMap<String, String> comments = new HashMap<String, String>();
			StringTokenizer st = new StringTokenizer(coms, "\r");
			while (st.hasMoreTokens()) {
				String[] res = st.nextToken().split(":");
				if (res.length > 1 && !res[0].equals("Cfg")) {
					comments.put(res[0].trim(), res[1].trim());
				}
			}
			den.setText(comments.get("Lbl"));
			date.setText(comments.get("Dat"));

		} catch (ImageProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
